package com.rmondjone.androidcamerademo

import android.graphics.Color
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.rmondjone.androidcamerademo.bean.CameraType
import com.rmondjone.androidcamerademo.bean.FaceBean
import com.rmondjone.camera.BaseActivity
import com.rmondjone.face.TipsDialog
import com.rmondjone.utils.StatusBarUtil
import com.rmondjone.utils.setClickListener
import kotlinx.android.synthetic.main.activity_face.*
import kotlinx.coroutines.*
import java.io.File

/**
 * copyright：
 * @author：anthui creation date： .
 * @since: 1.0.0
 * @describe:
 */
class FaceActivity : BaseActivity() {
    lateinit var faceAdapter: FaceAdapter

    var isLoadFinish = false
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_face)
        StatusBarUtil.setStatusBarDarkFontMode(this, Color.TRANSPARENT)

        val imagePath = intent.getStringExtra("path")
        face_view.setImage(imagePath)

//        iv_sao.startAnimator()

        layout_top.setClickListener {
            MainActivity.isClick = false
            MainActivity.startActivity(this@FaceActivity, CameraType.CAMERA_TYPE_XUE_WEI)
            finish()
        }


        face_view.onCallBack = object : FaceView.OnMessageCallBack {
            override fun onCallBack(content: FaceBean.ApdataBean.ListBean) {


                tv_title.text = content.name
                tv_contents.text = ""

                if (content.info == null) {
                    return
                }
                val build = StringBuilder()

                if (!TextUtils.isEmpty(content.info.meridians)) {
                    build.append("所属经脉：")
                    build.append(content.info.meridians)
                }


                if (!TextUtils.isEmpty(content.info.location)) {
                    build.append("\n位置：")
                    build.append(content.info.location)
                }
                if (!TextUtils.isEmpty(content.info.disease)) {
                    build.append("\n主调病症：\n")
                    build.append(content.info.disease)
                }
                tv_contents.text = build.toString()

            }

        }
        recyclerview.apply {
            faceAdapter = FaceAdapter()
            var layout = LinearLayoutManager(this@FaceActivity)
            layout.orientation = LinearLayoutManager.HORIZONTAL
            layoutManager = layout
            adapter = faceAdapter
            faceAdapter.setOnItemClickListener { adapter, view, position ->

                apdataBean?.isCheck = false
                var any = faceAdapter.data[position]
                face_view.changePosition(any)
                any.isCheck = true
                apdataBean = any
                faceAdapter.notifyDataSetChanged()

            }

        }
        var intExtra = intent.getIntExtra("typeId", 7)
        MainViewModel.upFile(File(imagePath), intExtra, object : MainViewModel.OnCallListener {
            override fun onCallBack(data: FaceBean) {


                data.apdata?.firstOrNull()?.apply {
                    apdataBean = this
                    isCheck = true
                }
                face_view.setFaceData(data)
                faceAdapter.setNewData(data.apdata)

                if (isLoadFinish) {
//                    iv_sao.stopAnimator()
                    layout_load.visibility = View.GONE
                }
                isLoadFinish = true

            }

            override fun onFail(message: String) {

                TipsDialog().apply {
                    tips = message
                    click = {
                        MainActivity.isClick = false
                        MainActivity.startActivity(this@FaceActivity, CameraType.CAMERA_TYPE_XUE_WEI)
                        finish()
                    }
                }.show(supportFragmentManager)
            }


        })

        startLoad()
    }


    private fun startLoad() {

        layout_load.visibility = View.VISIBLE
//        Glide.with(this).load(R.drawable.loading).into(iv_load)
//
//        正在五官识别...
//        正在体质解析...
//        正在生成您的分析报告
        GlobalScope.launch {
            tv_load.text = "正在识别穴位..."
            delay(1000)
            withContext(Dispatchers.Main) {
                tv_load.text = "正在识别穴位..."
            }
            delay(1000)
            withContext(Dispatchers.Main)
            {
                tv_load.text = "正在生成您的分析报告..."
            }
            delay(1000)

            if (isLoadFinish) {
//                iv_sao.stopAnimator()
                withContext(Dispatchers.Main) {
                    layout_load.visibility = View.GONE
                }
//                layout_load.visibility = View.GONE
            }
            isLoadFinish = true

        }
    }

    var apdataBean: FaceBean.ApdataBean? = null
    override fun onBackPressed() {
//        super.onBackPressed()
        MainActivity.isClick = false
        MainActivity.startActivity(this@FaceActivity, CameraType.CAMERA_TYPE_XUE_WEI)
        finish()
    }
}